package com.hutiao.snowhb.actions.wechat;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.springframework.beans.factory.annotation.Autowired;

import com.hutiao.snowhb.common.MongoUtil;
import com.hutiao.snowhb.entities.PhotographerBean;
import com.hutiao.snowhb.resource.ConfigInfo;
import com.hutiao.snowhb.service.PhotographerService;
import com.hutiao.snowhb.wechat.tools.WeiXinTools;
import com.hutiao.snowhb.wechat.vo.WxRecvPicMsg;
import com.hutiao.snowhb.wechat.vo.WxSendMsg;
import com.hutiao.snowhb.wechat.vo.WxSendTextMsg;
import com.opensymphony.xwork2.ActionSupport;

public class HandlePicMsgAction extends ActionSupport implements ServletRequestAware {

	private static final long serialVersionUID = -3485428114165386177L;

	private static Logger logger = Logger.getLogger(HandlePicMsgAction.class);

	private HttpServletRequest request;

	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}
	
	private String result;
	
	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}
	
	@Autowired
	private PhotographerService photographerService;

	public PhotographerService getPhotographerService() {
		return photographerService;
	}

	public void setPhotographerService(PhotographerService photographerService) {
		this.photographerService = photographerService;
	}
	
	
	
	@Action(value="handlepicmsg",
			results = {
				@Result(name="success",type="xmlstr", params={"stringName", "result"})})
	public String execute() throws Exception {
		
		logger.info("msg type is WxRecvPicMsg");
		
		WxRecvPicMsg recvMsg = (WxRecvPicMsg) this.request.getAttribute("recvMsg");
		
//		StringBuffer buffer = new StringBuffer();  
//	    buffer.append("感谢您上传照片参加“我是摄影师”微信建筑摄影活动。").append("\n\n");
//	    buffer.append("现在您仅需以“标题#图片说明”的格式发送文字信息，即可完成参赛！");
//	    String echo = buffer.toString();
//		String key = "photographer_" + recvMsg.getFromUser();
//		
//		PhotographerBean bean = MongoUtil.findSession(key);
//		bean.setOpenId(recvMsg.getFromUser());
//		bean.setPhotoUrl(recvMsg.getPicUrl());
//		bean.setMediaId(recvMsg.getMediaId());
//		
//		if (bean.getPhotoTitle() != null && !bean.getPhotoTitle().equals("")) {
//			try {
//				int photoId = this.photographerService.saveUserPhoto(bean);
//				String redirectUrl = ConfigInfo.hdPhotoDetailUrl+"?photoId="+photoId;
//				echo = "感谢您的参与，您的作品我们已收到。 <a href=\""+redirectUrl+"\">点击查看</a>";
//				MongoUtil.removeSession(key);
//			} catch (Exception e) {
//				logger.error(e.getMessage());
//				echo = "对不起，作品保存出现了一点问题，请您稍后再试。";
//			}
//		} else {
//			MongoUtil.saveSession(bean);
//			logger.info("session put ok");
//		}
		
//		StringBuffer buffer = new StringBuffer();
//		buffer.append("感谢您参与雪花“微园林集赞”活动。稍后我们会统计你的赞数，如果中奖我们会第一时间回复你哦，请密切关注雪花湖北微信动态。\n");
//		String echo = buffer.toString();
		
//		// 返回下一步的操作提示
//		WxSendMsg sendMsg = WeiXinTools.builderSendByRecv(recvMsg);
//		sendMsg = new WxSendTextMsg(sendMsg, echo);
//		this.result = WeiXinTools.getSendXmlStr(sendMsg);
		
		return SUCCESS;
	}
	
}
